<template>
	<u-no-network></u-no-network>
	<view class="sun-index">
		<view class="sun-logo-box">
			<view class="sun-logo">
				<image class="sun-icon-img" src="@/static/imgs/fire_green.png" />
			</view>
		</view>
		<view class="sun-login-box">
			<view class="sun-label">
				<image style="width: 28rpx;height:39rpx;" src="@/static/imgs/mobile_icon.png" />
				<text class="label-text">账号</text>
			</view>
			<view class="sun-input-box">
				<input v-model="uid" type="text" placeholder="请输入账号" placeholder-class="placeholder-class" />
				<image @click="uid=''" class="close-icon" src="@/static/imgs/close_icon.png" />
			</view>
		</view>
		<view class="sun-login-box">
			<view class="sun-label">
				<image style="width: 29rpx;height:37rpx;" src="@/static/imgs/pwd_icon.png" />
				<text class="label-text">密码</text>
			</view>
			<view class="sun-input-box">
				<input v-model="pwd" type="password" placeholder="请输入密码" placeholder-class="placeholder-class" />
				<image @click="pwd=''" class="close-icon" src="@/static/imgs/close_icon.png" />
			</view>
		</view>
		<view class="sun-tip">
			<u-checkbox v-model="savePwd" @change="savePwdChange" class="sun-tip-text">记住密码</u-checkbox>
			<u-checkbox v-model="autoLogin" @change="autoLoginChange" class="sun-tip-text">自动登录</u-checkbox>
			<!-- <text class="sun-tip-text">忘记密码/解绑</text> -->
			<!-- <text class="sun-tip-text" @click="goRegister">注册</text> -->
		</view>
		<view class="sun-tip">
			<view class="set-tip-text" @click="setHost"><u-icon name="setting"></u-icon> 设置服务器</view>
			<view class="set-tip-text" @click="forgetPwd">忘记密码？</view>
		</view>
		<view class="login-btn-box">
			<view class="login-btn" @click="handleSubmit">登录</view>
		</view>
		<view class="register-btn-box">
			<view class="login-btn" @click="registerUser" style="background-color: #0080ff;">注册</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				uid: '',
				pwd: '',
				savePwd: true,
				autoLogin: true,
				isNavgeto: false
			}
		},
		onShow() {
			var _pwd = uni.getStorageSync('pwd');
			if (_pwd)
				this.pwd = _pwd;
			var _code = uni.getStorageSync('usercode')
			this.uid = _code;
			var _save = uni.getStorageSync('savePwd');
			if (_save)
				this.savePwd = _save;
			var _autoLogin = uni.getStorageSync('autoLogin');
			if (_autoLogin && this.uid && this.pwd) {
				this.autoLogin = _autoLogin;
				if (this.autoLogin && !this.isNavgeto)
					this.handleSubmit();
			}
			console.log(uni.getStorageSync('usercode'))
		},
		onLoad: function(option) {
			console.log(option.isNavgeto)
			if (undefined != option.isNavgeto)
				this.isNavgeto = option.isNavgeto
		},
		methods: {
			autoLoginChange(e) {
				this.autoLogin = e.value;
			},
			savePwdChange(e) {
				this.savePwd = e.value;
				console.log(this.savePwd)
			},
			handleSubmit() {
				if (!this.uid) return this.$u.toast('请输入账号')
				if (!this.pwd) return this.$u.toast('请输入密码')
				uni.showLoading({
					title: "登录中...",
					mask: true
				});
				uni.switchTab({
					url: '/pages/mine/mine',
					success() {
						uni.hideLoading();
					}
				});
				this.$request('User/CheckUser', 'GET', {
					userCode: this.uid,
					userPwd: this.pwd
				}).then(res => {
					console.log(res)
					if (res.data) {
						this.$u.toast('登录成功');
						if (this.savePwd)
							uni.setStorageSync('pwd', this.pwd);
						uni.setStorageSync('savePwd', this.savePwd);
						uni.setStorageSync('autoLogin', this.autoLogin);
						uni.setStorageSync('usercode', this.uid);
						this.$globalState.S1600Info.Login = true;
						this.$globalState.S1600Info.UserCode = this.uid;
						console.log(uni.getStorageSync('usercode'))
						uni.hideLoading();
						uni.switchTab({
							url: '/pages/mine/mine'
						});

					} else {
						uni.hideLoading();
						this.$u.toast('登录失败，请联系管理员');
						return;
					}
				}).catch(error=>{
					uni.hideLoading();
					this.$u.toast('网络异常！请先配置服务器或检查网络');
					return;
					console.log(error)
				})
			},
			registerUser() {
				uni.navigateTo({
					url: '/pages/login/register'
				})
			},
			forgetPwd() {
				this.$u.toast('联系电话:0731-85869788');
			},
			setHost() {
				uni.navigateTo({
					url: '/pages/login/host'
				})
			}
		}
	}
</script>

<style>
	@import url("login.css");
</style>